/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package perso;

import carte.Carte;
import ctrl.command.CmdConvert;
import ctrl.command.CmdCreate;
import ctrl.command.CmdKill;
import ctrl.command.CmdMove;
import ctrl.command.Command;
import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Benoît
 */
public class Yoda extends PersonnageStarWars {

    private static volatile Yoda instance = null;

    private Yoda(int id, Carte carte) {
        super(id, carte);
        setImage("/images/yoda.png");
        name = "Yoda";
    }

    public static Yoda getInstance(int id, Carte carte) {

        if (Yoda.instance == null) {

            synchronized (Yoda.class) {
                if (Yoda.instance == null) {
                    Yoda.instance = new Yoda(id, carte);
                }
            }
        }
        return Yoda.instance;
    }

    @Override
    public Command doAction() {
        Random rnd = new Random();
        
        Command cmd = new CmdMove(this, px, py);
        if((rnd.nextInt(100) > 50) && (px == 1 && py == 1)){
            cmd = new CmdCreate(this, px, py);
        }
        
        int lower = 1;
        int higher = 5;
        int random = (int) (Math.random() * (higher - lower)) + lower;
        int pxDest = px;
        int pyDest = py;
        if (random == 1) {
            if (px + 1 < carte.getPerso().length) {
                pxDest++;
            }
        } else if (random == 2) {
            if (px - 1 > 0) {
                pxDest--;
            }
        } else if (random == 3) {
            if (py - 1 > 0) {
                pyDest--;
            }
        } else if (random == 4) {
            if (py + 1 < carte.getPerso()[0].length) {
                pyDest++;
            }
        }

        
        Personnage cible = carte.getPerso()[pxDest][pyDest];
        if (cible == null) {
            cmd = new CmdMove(this, pxDest, pyDest);
        } else if (cible instanceof DarkVador) {
            if (rnd.nextInt(100) > 50) {
                cmd = new CmdKill(cible);
            } else {
                cmd = new CmdKill(this);
            }
        } else if (cible instanceof Sith) {
            if (rnd.nextInt(100) > 25) {
                if (rnd.nextInt(100) > 50) {
                    cmd = new CmdConvert(cible);
                } else {
                    int esquivePx = 0;
                    int esquivePy = 0;
                    try {
                        Personnage esquive = carte.getPerso()[cible.px - 1][cible.py - 2];
                        if (esquive == null) {
                            esquivePx = cible.px - 1;
                            esquivePy = cible.py - 2;
                        }

                        esquive = carte.getPerso()[cible.px - 1][cible.py + 2];
                        if (esquive == null) {
                            esquivePx = cible.px - 1;
                            esquivePy = cible.py + 2;
                        }

                        esquive = carte.getPerso()[cible.px + 1][cible.py - 2];
                        if (esquive == null) {
                            esquivePx = cible.px + 1;
                            esquivePy = cible.py - 2;
                        }

                        esquive = carte.getPerso()[cible.px + 1][cible.py + 2];
                        if (esquive == null) {
                            esquivePx = cible.px + 1;
                            esquivePy = cible.py + 2;
                        }
                    } catch (ArrayIndexOutOfBoundsException e) {
                        // esquive out of map
                        esquivePx = esquivePy = 0;
                    }

                    if (esquivePx != 0 && esquivePy != 0) {
                        cmd = new CmdMove(cible, esquivePx, esquivePy);
                    } else {
                        cmd = new CmdKill(cible);
                    }
                }
            } else {
                cmd = new CmdConvert(cible);
            }
        } else if (cible instanceof ImperialSoldier) {
            if (rnd.nextInt(100) > 5) {
                cmd = new CmdKill(cible);
            } else {
                cmd = new CmdKill(this);
            }
        } else if (cible instanceof RebelPilot) {
            cmd = new CmdConvert(cible);
        }

        return cmd;

    }

    @Override
    public void convert(ArrayList<Personnage> listPerso) {
        
    }
}
